package com.zz;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.zz.POJO.Family;
import com.zz.POJO.Person;

import java.util.ArrayList;
import java.util.List;

/**
 * java对象转换为json测试类
 */
public class FastJsonTest {

    public void pojoTest() {
        Person person = new Person(18, "man", "zz");
        System.out.println(JSON.toJSON(person));
        System.out.println(JSONObject.toJSONString(person));
    }

    public void listTest() {
        Family family = new Family();
        family.setAddress("三店");
        family.setCity("武汉市");
        List<Person> personList =new ArrayList<>();
        Person person1 = new Person(18, "man", "zz");
        personList.add(person1);
        Person person2 = new Person(19, "man", "zz2");
        personList.add(person2);
        family.setPersons(personList);
        System.out.println(JSONObject.toJSONString(family,SerializerFeature.WriteMapNullValue));
        System.out.println(JSON.toJSON(family));
    }

    public static void main(String[] args) {
        FastJsonTest fastJsonTest = new FastJsonTest();
        fastJsonTest.pojoTest();
        fastJsonTest.listTest();
    }
}
